package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.sky.constant.StatusConstant.ENABLE;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        System.out.println("dddddddddddddddddddd");
        System.out.println(dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors == null || flavors.isEmpty()) {
            return;
        }
        System.out.println("ffffffffffffffffffffffffff");
        System.out.println(dishId);
        flavors.forEach(flavor -> flavor.setDishId(dishId));
        dishFlavorMapper.insertBatch(flavors);
        System.out.println("hhhhhhhhhhhhhhhhhhhhhh");
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dish) {
        PageHelper.startPage(dish.getPage(), dish.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dish);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
//        正在出售的菜品不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (Objects.equals(dish.getStatus(), ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
//        关联了套餐的菜品不能删除
        List<Long> setMealIds = setmealDishMapper.getByDishId(ids);
        if (!setMealIds.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        ids.forEach(id -> dishMapper.deleteById(id));
        ids.forEach(id -> dishFlavorMapper.deleteByDishId(id));
    }

    @Override
    public void updateStatus(int status, Long id) {
        Dish dish = dishMapper.getById(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> getById(Long id) {
        List<Dish> dish = dishMapper.selectById(id);
        return dish;

    }

    @Override
    @Transactional
    public void update(DishDTO dto) {
//        先把原来每个dish关联的口味全部删除
        dishFlavorMapper.deleteByDishId(dto.getId());
//    再把修改之后的dish关联的口味插入表中

        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor flavor : flavors) {
//            先得把每个口味所属的dishid设置一下
            flavor.setDishId(dto.getId());
            System.out.println(flavor);
            dishFlavorMapper.save(flavor);
        }
//然后就是直接修改单张dish表
        Dish dish = new Dish();

        BeanUtils.copyProperties(dto, dish);
        System.out.println(dish.getName());
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> list(Long categoryId) {
        List<Dish> dishList = dishMapper.selectById(categoryId);
//        过滤掉停售中的菜品
        dishList = dishList.stream().filter(dish -> Objects.equals(dish.getStatus(), ENABLE)).collect(Collectors.toList());
        List<DishVO> dishVOList = new ArrayList<>();
        for(Dish dish:dishList){
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(dish.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
